$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'flow/flowcustomergroups/list',
        datatype: "json",
        colModel: [
            {label: '会员组id', name: 'id', index: 'id', width: 50, key: true},
            {label: '会员组名字', name: 'name', index: 'name', width: 80},
            {
                label: '分组类别', name: 'companyId', index: 'company_id', width: 80,
                formatter: function (value, options, row) {
                    return value === 1 ?
                        '<span class="label label-success" value="1">分组类别</span>' :
                        '<span class="label label-danger" value="0">分组类别</span>'
                }
            },
            {label: '会员人数', name: 'customerCount', index: 'customer_count', width: 80},
            {
                label: '照片', name: 'bFlowCustomers', index: 'b_flow_customers', width: 80,
                formatter: function (value, options, row) {
                    str = '';
                    for (var i = 0; i < value.length; i++) {
                        var url = value[i].originalFaceUrl;
                        str += "<img src=" + url + " style: height='40' width='40'>"
                    }
                    return str;
                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "bFlowCustomerGroups.list",
            page: "bFlowCustomerGroups.pageNum",
            total: "bFlowCustomerGroups.pages",
            records: "bFlowCustomerGroups.total"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        bFlowCustomerGroups: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        customer: function () {
            var url = "flow/flowcustomergroups/customer";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.bFlowCustomers),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.bFlowCustomerGroups = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.bFlowCustomerGroups.id == null ? "flow/flowcustomergroups/save" : "flow/flowcustomergroups/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.bFlowCustomerGroups),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            console.log("ids:::", ids)
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "flow/flowcustomergroups/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "flow/flowcustomergroups/info/" + id, function (r) {
                vm.bFlowCustomerGroups = r.flowCustomerGroups;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});